<?php
/*
 * @分销商城
 * @功能说明：模板消息接口
 * @更新说明：暂无更新
 * @文件名 JsssdkController.class.php
 * @编码 UTF-8
 * @创建时间 2016-5-17 下午2:22:50
 * @创建人 BIELANG
 */

namespace wap\Controller;
use Think\Controller;
class JsssdkController extends Controller
{
	/*
    * 分销商城_微信模板消息(WENCUN改于2016-05-26 18:16:00)
    * @date: 2016-5-24 下午4:52:01
    * @editor: BIELANG
    */
	public function buyerNew(){
		//获取买家wxsek值
		$getBuyerWxsek = M('CustEntity')->getFieldByCustId(I('post.cust_id'),'wxsek');
		$orderId = I('post.order_id');
		$cashMoney = I('post.cashMoney');
		$goodsTitle = I('post.goods_title');
		$url ='http://wap.hipaygo.cn/order/odetail/orderId/'.I('post.order_id');
		//买家购买商品推送模板
		$buyerInfo = array(
			'first' =>array('value'=>'感谢您，成功购买了嗨呗的商品','color'=>'#743A3A'),
			'keyword1'=>array('value'=>$goodsTitle,'color'=>'#743A3A'),
			'keyword2'=>array('value'=>number_format($cashMoney,2,'.','').'元','color'=>"#743A3A"),
			'keyword3'=>array('value'=>date('Y-m-d H:i:s',time()),'color'=>"#743A3A"),
			'remark'=>array('value'=>'点击查看订单详情','color'=>'#743A3A')
		);

		//买家购买通知
		self::msg('s60xY5eIYkoxHHp9X83BAP2bdHDE_g99fsETu7hGyjQ',$buyerInfo,$getBuyerWxsek,$url);
		//升级推送
		if(I('post.ltype') && in_array(I('post.ltype'),array('1','2','3'))){
			//用户昵称
			$getBuyerNickname = M('CustEntity')->getFieldByCustId(I('post.cust_id'),'nickname');;
			//买家升级模板
			$levelInfo = array(
				'first' => array('value'=>'恭喜你的等级升级了','color'=>'#743A3A'),
				'keyword1' => array('value'=>$getBuyerNickname,'color'=>'#743A3A'),
				'keyword3' => array('value'=>date('Y-m-d H:i:s',time()),'color'=>'#743A3A'),
			);
			if(I('post.ltype') == '1'){
				//升级店长
				$levelInfo['keyword2'] = array('value'=>'嗨呗店长','color'=>'#743A3A');
			}else if(I('post.ltype') == '2'){
				//升级经理
				$levelInfo['keyword2'] = array('value'=>'嗨呗经理','color'=>'#743A3A');
			}else if(I('post.ltype') == '3'){
				//店长升级经理
				$levelInfo['keyword2'] = array('value'=>'嗨呗经理','color'=>'#743A3A');
			}
			//升级用户发送通知
			self::msg('yI1_HNoB6o4hKB0gx2d5eSgwRBx6QjfJ0_IkZX79YMA',$levelInfo,$getBuyerWxsek);
			//如果不是顶级用户则推送奖金
			if(!I('post.is_top')){
				//获取推荐人wxsek
				$getBizWxsek = M('CustEntity')->getFieldByCustId(I('post.biz_cust_id'),'wxsek');
				//如果卖家在自己店铺买OEM商品则给卖家的上级推送消息
				if(I('post.biz_cust_id') == I('post.cust_id')){
					$getSjId = M('CustEntity')->getFieldByCustId(I('post.biz_custid'),'parent_id');
					$getBizWxsek = M('CustEntity')->getFieldByCustId($getSjId,'wxsek');
				}
				$profit = array(
					'1' => array(
						'150','500','200','1'//店长(588) 1嗨币
					),
					'2' => array(
						'160','600','300','1'//经理(1588) 1嗨币
					),
					'3' => array(
						'160','600','300','1'//总监 1嗨币
					)
				);
				//间推收益
				$indirectProfit = array(
					'30','50'
				);
				//总监有上级总监收益
				$profitZj = array(
					'130','150'
				);
				//总监上级总监收益
				$superiorProfit = array(
					'20','30'
				);

				//(奖金)直推升级模板
				$ztLevelInfo = array(
					'first' =>array('value'=>'您好,您有一笔收入进账啦','color'=>'#743A3A'),
					'keyword1'=>array('value'=>'推荐奖金','color'=>'#743A3A'),
					'keyword2'=>array('value'=>$profit[I('post.biz_cust_salelevel')][I('post.ltype')-1].'元','color'=>"#743A3A"),
					'keyword3'=>array('value'=>date('Y-m-d H:i:s',time()),'color'=>"#743A3A"),
				);

				//直推用户奖金通知
				self::msg('o_iMHfQ2ar4oGgosDa5I7yP7VKX3eXssMSccRZvYCIE',$ztLevelInfo,$getBizWxsek);

				if(I('post.ltype') != '3'){
					//经理间推奖金
					$jlId = (new InlevelController())->findJl(I('post.biz_cust_id'));
					if($jlId){
						//获取经理的wxsek值
						$getJlWxsek = M('CustEntity')->getFieldByCustId($jlId,'wxsek');
						//经理间推奖金通知
						if(I('post.ltype') != '3'){
							$ztLevelInfo['keyword2'] = array('value'=>$indirectProfit[I('post.ltype')-1].'元','color'=>"#743A3A");
							self::msg('o_iMHfQ2ar4oGgosDa5I7yP7VKX3eXssMSccRZvYCIE',$ztLevelInfo,$getJlWxsek);
						}
					}else{
						//如果直推人是经理并且没有上级经理则享受上级经理的间推奖金
						if(I('post.biz_cust_salelevel') == '2'){
							$ztLevelInfo['keyword2'] = array('value'=>$indirectProfit[I('post.ltype')-1].'元','color'=>"#743A3A");
							self::msg('o_iMHfQ2ar4oGgosDa5I7yP7VKX3eXssMSccRZvYCIE',$ztLevelInfo,$getBizWxsek);
						}
					}
					$zjId = (new InlevelController())->findZj(I('post.biz_cust_id'));
					//总监奖金
					if($zjId){
						$getZjWxsek = M('CustEntity')->getFieldByCustId($zjId,'wxsek');
						$zjGroupInfo = array(
							'first' =>array('value'=>'您好,您有一笔收入进账啦','color'=>'#743A3A'),
							'keyword1'=>array('value'=>'团队奖金','color'=>'#743A3A'),
							'keyword2'=>array('value'=>$profitZj[I('post.ltype')-1].'元','color'=>"#743A3A"),
							'keyword3'=>array('value'=>date('Y-m-d H:i:s',time()),'color'=>"#743A3A"),
						);
						//总监上级总监奖金
						$findZj = (new InlevelController())->zjFindZj($zjId);
						if($findZj){
							//todo 判断上级总监类型
							$zjType = M('CustEntity')->getFieldByCustId($findZj,'zj_type');
							if($zjType == '2'){
								//总监奖金通知
								self::msg('o_iMHfQ2ar4oGgosDa5I7yP7VKX3eXssMSccRZvYCIE',$zjGroupInfo,$getZjWxsek);
								//获取总监上级总监的wxsek值
								$getZjZjWxsek = M('CustEntity')->getFieldByCustId($findZj,'wxsek');
								//总监上级总监奖金通知
								$zjGroupInfo['keyword2'] = array('value'=>$superiorProfit[I('post.ltype')-1].'元','color'=>"#743A3A");
								self::msg('o_iMHfQ2ar4oGgosDa5I7yP7VKX3eXssMSccRZvYCIE',$zjGroupInfo,$getZjZjWxsek);
							}else{
								$zjGroupInfo['keyword2'] = array('value'=>$profitZj[I('post.ltype')-1]+$superiorProfit[I('post.ltype')-1].'元','color'=>"#743A3A");
								self::msg('o_iMHfQ2ar4oGgosDa5I7yP7VKX3eXssMSccRZvYCIE',$zjGroupInfo,$getZjWxsek);
							}
						}else{
							$zjGroupInfo['keyword2'] = array('value'=>$profitZj[I('post.ltype')-1]+$superiorProfit[I('post.ltype')-1].'元','color'=>"#743A3A");
							self::msg('o_iMHfQ2ar4oGgosDa5I7yP7VKX3eXssMSccRZvYCIE',$zjGroupInfo,$getZjWxsek);
						}
					}
				}
			}
		}else{
			//如果OEM商品则没有收益
			if(!I('post.oemPrice')){
				foreach(I('post.biz_cust_id') as $key=>$value){
					//获取每个卖家的wxsek
					$profit = 0;
					foreach($value as $v){
						$profit += $v;
					}
					$getBizWxsek = M('CustEntity')->getFieldByCustId($key,'wxsek');
					//卖家店铺收益推送模板
					$bizInfo = array(
						'first' =>array('value'=>'您好，您有一笔收入进账啦','color'=>'#743A3A'),
						'keyword1'=>array('value'=>'店铺订单收入','color'=>'#743A3A'),
						'keyword2'=>array('value'=>number_format($profit,2,'.','').'元','color'=>"#743A3A"),
						'keyword3'=>array('value'=>date('Y-m-d H:i:s',time()),'color'=>"#743A3A"),
						'remark'=>array('value'=>'点击查看订单详情','color'=>'#743A3A')
					);
					//总监团队销售
					$zjId = (new ProfitController())->findZj($key);
					if($zjId){
						//获取总监wxsek值
						$getZjMsg = M('CustEntity')->field(array('wxsek','zj_type'))->where(array('cust_id'=>$zjId))->find();
						//总监团队销售利润推送模板
						$zjInfo = array(
							'first' =>array('value'=>'您好，您有一笔收入进账啦','color'=>'#743A3A'),
							'keyword1'=>array('value'=>'团队销售','color'=>'#743A3A'),
							'keyword3'=>array('value'=>date('Y-m-d H:i:s',time()),'color'=>"#743A3A"),
							'remark'=>array('value'=>'点击查看订单详情','color'=>'#743A3A')
						);
						if($getZjMsg['zj_type'] == '1'){
							$zjInfo['keyword2'] = array('value'=>number_format($profit*0.2,2,'.','').'元','color'=>"#743A3A");
						}else if($getZjMsg['zj_type'] == '2'){
							$zjInfo['keyword2'] = array('value'=>number_format($profit*0.4,2,'.','').'元','color'=>"#743A3A");
						}
						self::msg('o_iMHfQ2ar4oGgosDa5I7yP7VKX3eXssMSccRZvYCIE',$zjInfo,$getZjMsg['wxsek'],$url);
					}
					//卖家收益通知
					self::msg('o_iMHfQ2ar4oGgosDa5I7yP7VKX3eXssMSccRZvYCIE',$bizInfo,$getBizWxsek,$url);
				}
			}
		}
	}

	/*
	 * 分销商城_升级嗨呗总监
	 * @date: 2016-06-01 15:10:00
	 * @editor: WENCUN
	 * */
	public function inlevelZj(){
		if(I('post.cust_id')){
			//获取总监WXSEK以及昵称
			$getZjMsg = M('CustEntity')->field(array('nickname','wxsek'))->where(array('cust_id'=>I('post.cust_id'),'salelevel'=>'3'))->find();
			//$getZjMsg['wxsek'] = 'oBRNQweigscGm0XHQRmkGvb-eGeg';
			$levelInfo = array(
				'first' => array('value'=>'恭喜你的等级升级了','color'=>'#743A3A'),
				'keyword1' => array('value'=>$getZjMsg['nickname'],'color'=>'#743A3A'),
				'keyword2' => array('value'=>'嗨呗总监','color'=>'#743A3A'),
				'keyword3' => array('value'=>date('Y-m-d H:i:s',time()),'color'=>'#743A3A'),
			);
			self::msg('yI1_HNoB6o4hKB0gx2d5eSgwRBx6QjfJ0_IkZX79YMA',$levelInfo,$getZjMsg['wxsek']);
		}
	}

	/*
	 * 分销商城_模板消息(提现)
	 * @date: 2016-05-26 18:39:00
	 * @editor: WENCUN
	 * */
	public function applyCash(){
		//提现用户
		$CustWxsek = M('CustEntity')->getFieldByCustId(I('post.cust_id'),'wxsek');
		//$CustWxsek = 'oBRNQweigscGm0XHQRmkGvb-eGeg';
		//提现金额
		$cashMoney = I('post.cash_money');
		if(I('post.cash_status') && I('post.cash_status') == '1' && I('post.is_check') && I('post.is_check') == '1'){
			//提现成功模板
			$cashInfo = array(
				'first' => array('value'=>'您好,您已提现,请注意查收','color'=>'#743A3A'),
				'money' => array('value'=>number_format($cashMoney,2,'.','').'元','color'=>'#743A3A'),
				'time' => array('value'=>date('Y-m-d H:i:s',time()),'color'=>"#743A3A"),
				'remark' => array('value'=>'如有问题请直接回复,我们客服将第一时间为您服务','color'=>'#743A3A'),
			);
			//提现成功消息发送
			self::msg('FoHgeJpWP0-2KyFTq71JwF9_6U0bLQwtASLQMLzF4SU',$cashInfo,$CustWxsek);
		}else if(I('post.is_check') == '-1'){
			$cashInfo = array(
				'first' => array('value'=>'提现申请失败,如有疑问请联系客服','color'=>'#743A3A'),
				'money' => array('value'=>number_format($cashMoney,2,'.','').'元','color'=>'#743A3A'),
				'time' => array('value'=>date('Y-m-d H:i:s',time()),'color'=>"#743A3A"),
				'remark' => array('value'=>'如有问题请直接回复,我们客服将第一时间为您服务','color'=>'#743A3A'),
			);
			self::msg('Ui7X6IWP8mSNoM0XEmv7nLRJ_Cu81_xFTsOTQS0k8eM',$cashInfo,$CustWxsek);
		}else{
			//申请提现模板
			$cashInfo = array(
				'first' =>array('value'=>'您有一条提现申请,请耐心等待审核','color'=>'#743A3A'),
				'keyword1'=>array('value'=>number_format($cashMoney,2,'.','').'元','color'=>'#743A3A'),
				'keyword2'=>array('value'=>date('Y-m-d H:i:s',time()),'color'=>"#743A3A"),
				'keyword3'=>array('value'=>'待审核','color'=>"#743A3A"),
				'remark' => array('value'=>'如有问题请直接回复,我们客服将第一时间为您服务','color'=>'#743A3A'),
			);
			//申请提现消息发送
			self::msg('8VoOL0EsWMhJ0BHwp4Mh5pByMY8gW9DOGuFmGckJzbE',$cashInfo,$CustWxsek);
		}
	}

	/*
	 * 分销商城_模板消息(退款通知)
	 * @date: 2016-05-26 19:01:00
	 * @editor: WENCUN
	 * */
	public function refund(){
		if(I('post.order_id')){
			$orderMsg = M('OrderEntity')->where(array('order_id'=>I('post.order_id'),'order_status'=>'8'))->find();
			$refundMoney = $orderMsg['order_amount'];
			$refundType = explode(',',getSysConfig('refundType'));
			$refundLy = $refundType[$orderMsg['refund_type']];
			$CustWxsek = M('CustEntity')->getFieldByCustId($orderMsg['cust_id'],'wxsek');
			//$CustWxsek = 'oBRNQweigscGm0XHQRmkGvb-eGeg';
			$refundInfo = array(
				'first' =>array('value'=>'您有一笔退款,请注意查收','color'=>'#743A3A'),
				'reason'=>array('value'=>$refundLy,'color'=>'#743A3A'),
				'refund'=>array('value'=>number_format($refundMoney,2,'.','').'元','color'=>'#743A3A'),
				'remark' => array('value'=>'如有问题请直接回复,我们客服将第一时间为您服务','color'=>'#743A3A'),
			);
			self::msg('6GPuoz4s_5XloBn4vaS3ow-7zWU2_2GVwM4Q4uj8PNY',$refundInfo,$CustWxsek);
		}
	}

	/*
	 * 分销商城_模板消息(商家入驻审核通知)
	 * @date: 2016-05-26 19:01:00
	 * @editor: WENCUN
	 * */
	public function salerCheck(){
		if(I('post.cust_id') && I('post.is_check')){
			$getCustMsg = M('CustEntity')->field(array('wxsek','cust_mobile','cust_realname','nickname'))->where(array('cust_id'=>I('post.cust_id')))->find();
			$custWxsek = $getCustMsg['wxsek'];
			//$custWxsek = 'oBRNQweigscGm0XHQRmkGvb-eGeg';
			$nickname = $getCustMsg['nickname'];
			$realname = $getCustMsg['cust_realname'];
			$custMobile = $getCustMsg['cust_mobile'];
			$checkLy = '';
			$checkTime = date('Y-m-d H:i:s',time());
			if(I('post.is_check') == '1'){
				$successInfo = array(
					//审核成功
					'first' => array('value'=>'恭喜您通过审核!','color'=>'#743A3A'),
					'keyword1' => array('value'=>$realname,'color'=>'#743A3A'),
					'keyword2' => array('value'=>$custMobile,'color'=>'#743A3A'),
					'keyword3' => array('value'=>$checkTime,'color'=>'#743A3A'),
					'remark' => array('value'=>'如有问题请直接回复,我们客服将第一时间为您服务!','color'=>'#743A3A'),
				);
				self::msg('uiLXBtQD5MEGegIRy59KtfCKynaVOA7aAzYRlcXiaZo',$successInfo,$custWxsek);
			}else{
				$failureInfo = array(
					//审核失败
					'first' => array('value'=>'很抱歉，您提交的代理资料经审批未获通过。','color'=>'#743A3A'),
					'keyword1' => array('value'=>$realname,'color'=>'#743A3A'),
					'keyword2' => array('value'=>$nickname,'color'=>'#743A3A'),
					'keyword3' => array('value'=>$custMobile,'color'=>'#743A3A'),
					'remark' => array('value'=>'如有问题请直接回复,我们客服将第一时间为您服务!','color'=>'#743A3A'),
				);
				self::msg('5YWvrLNF2uYqB2RmQy0pRWk_ei1r45lY62Ql8ANmYZs',$failureInfo,$custWxsek);
			}
		}
	}

	/*
	 * 分销商城_微信模板消息发送
	 * @date: 2016-05-24 下午4:15:00
	 * @editor: BIELANG
	 * */
    public function msg($template_id,$info,$touser,$url = '',$topcolor = '#7B68EE'){
        $template = array(
            'touser' => $touser,
            'template_id' => $template_id,
            'url' => $url,
            'topcolor' => $topcolor,
            'data' => $info
        );
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx96f4e32fe927be19&secret=edfdb7056ff5fac9a74897c7256b6b25";
		$res = json_decode(curl($url),true);
		$token = $res['access_token'];
        $json_template = json_encode($template);
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
		curl($url,$json_template);
    }
}
